import { Injectable } from '@angular/core';
import { LoadingController, AlertController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { HttpProvider } from '../http/http';
import 'rxjs/add/operator/map';

@Injectable()
export class HttpServiceProvider {
	loading: any;

  	constructor(public http: HttpProvider, 
  				public loadingCtrl: LoadingController, 
  				public storage: Storage,
		  		public alertCtrl: AlertController) {
    	console.log('Hello HttpServiceProvider Provider');
  	}

    //我的钱包
    mewallet(callback){
      this.http.authGet('/me/wallet').then((result) => {
        if(result['status']==200){
          callback(result)
        }else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    //钱包种类
    bcoinlist(callback){
      this.http.get('/property/bcoinlist').then((result) => {
        if(result['status']==200){
          callback(result)
        }else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    //对账单列表
    balancelist(callback){
      this.http.authGet('/balance/list').then((result) => {
        if(result['status']==200){
          callback(result)
        }else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    // 提现
    tixian(params, callback){
      this.http.authPost('/balance/tixian', params).then((result) => {
        if(result['status']==200){
          callback(result)
        }else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    // 充值
    chongzhi(params, callback){
      this.http.authPost('/balance/recharge', params).then((result) => {
        if(result['status']==200){
          callback(result)
        }else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    // 活动列表
    actlist(params, callback){
      this.http.post('/act/actlist', params).then((result) => {
        if(result['status']==200){
          callback(result)
        }
        else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    // 活动详情, id为记录主键
    actDetail(id, callback){
      this.http.get('/act/actDetail?id='+id).then((result) => {
        if(result['status']==200){
          callback(result)
        }
        else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

    // 首页活动列表
    homeActlist(callback){
      this.http.get('/act/appActlist').then((result) => {
        if(result['status']==200){
          callback(result)
        }
        else{
          this.show(result['msg'])
          callback(null)
        }
      },(err)=>{

      })
    }

  	// 登录
  	login(params, callback){
  		this.http.post('/user/signin', params).then((result) => {
  			if(result['status']==200){
  				this.storage.set('token', result['token']);
  				callback(result)
  			}
  			else{
  				this.show(result['msg'])
  				callback(null)
  			}
  		},(err)=>{

  		})
  	}

  	// 发送注册验证码
  	sendsms(params, callback){
  		this.http.post('/user/send', params).then((result) => {
  			if(result['status']==200)
  				callback(result)
  			else{
  				this.show(result['msg'])
  				callback(null)
  			}
  		},(err)=>{

  		})
  	}

  	// 注册
  	register(params, callback){
  		this.http.post('/user/register', params).then((result) => {
  			if(result['status']==200){
  				this.showLoader(result['msg'])
  				callback(result)
  			}
  			else{
  				this.show(result['msg'])
  				callback(null)
  			}
  		},(err)=>{

  		})
  	}

  	// 忘记密码
  	forget(params, callback){
  		this.http.post('/user/forget', params).then((result) => {
  			if(result['status']==200){
  				this.showLoader(result['msg'])
  				callback(result)
  			}
  			else{
  				this.show(result['msg'])
  				callback(null)
  			}
  		},(err)=>{

  		})
  	}

  	show(text) {
      	let alert = this.alertCtrl.create({
        	subTitle: text,
        	buttons: ['确定']
     	});
      	alert.present(prompt);
  	}

  	showLoader(content){
	    this.loading = this.loadingCtrl.create({
	        content: content
	    });
	    this.loading.present();
	    setTimeout(() => {
		    this.loading.dismiss();
		}, 2000);
  	}




}
